/**
 * 
 */
package com.jie.eclipse.pan.swtitem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.editors.form.PllDetailPage;
import com.jie.eclipse.pan.listener.PModifyListener;
import com.jie.eclipse.pan.listener.PMouseListener;
import com.jie.eclipse.pan.listener.PSelectionListener;
import com.jie.eclipse.pan.swtitem.actiongroup.PageActionGroup;
import com.jie.eclipse.pan.swtitem.actiongroup.SeachActionGroup1;
import com.jie.eclipse.pan.swtitem.actiongroup.SelectedColumnsActionGroup;
import com.jie.eclipse.pan.swtitem.actiongroup.TableActionGroup;
import com.jie.eclipse.pan.swtitem.actiongroup.TableColumnActionGroup;
import com.jie.eclipse.pan.swtitem.table.PTable;
import com.jie.eclipse.pan.swtitem.table.PTableColumn;
import com.jie.eclipse.pan.swtitem.table.PTableViewer;
import com.jie.eclipse.pan.swtitem.table.PllTableContentProvider;
import com.jie.eclipse.pan.swtitem.table.PllTableLabelProvider;
import com.jie.eclipse.pan.swtitem.table.cellmodifier.SelectColumnsTableCellModifier;
import com.jie.eclipse.pan.swtitem.widgets.BaseComposite;
import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.utils.Utils;
import com.jie.eclipse.pan.xml.XMLNode;

/**
 * @author yanjie
 *
 */
public class SWTHelper {
	private PllDetailPage detailPage = null;
	private Map<String ,BaseComposite> repeatCp = null;//table tablecolumn
	private Map<String ,BaseComposite> ctrMap = null;//table tablecolumn
	/**
	 * 
	 */
	public SWTHelper(PllDetailPage detailPage) {
		this.detailPage = detailPage;
		this.repeatCp = detailPage.getCpsMap();
	}
	
	public BaseComposite getBaseComposite(XMLNode node){
		String nodeName = node.getNodeName();
		if("page".equalsIgnoreCase(nodeName)){
			return getPageComposite(node);
		}else if("table".equalsIgnoreCase(nodeName)){
			return getTableComposite(node);
		}else if("tablecolumn".equalsIgnoreCase(nodeName)){
			return getTableColumnComposite(node);
		}
		
		return new BaseComposite(node, new Composite((Composite)detailPage.getDetailSection().getClient(), SWT.NONE));
	}
	
	/**
	 * @param table
	 * @return
	 * 返加表格栏位节点面板
	 */
	public BaseComposite getTableColumnComposite(XMLNode column){
		String id = column.getID();
		if(StringHelper.IsNullOrEmpty(id))
			id = column.gpsv("id");
		if(repeatCp.containsKey(id)){
			BaseComposite columnComposite = this.repeatCp.get(id);
			return columnComposite;
		}else{
			Color bgcolor = detailPage.getDetailSection().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
			FormToolkit toolkit = detailPage.getMform().getToolkit();
			ViewForm vform = new ViewForm((Composite)detailPage.getDetailSection().getClient(), SWT.NONE);
			vform.setLayout(new GridLayout(1, true));
			GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
			vformData.horizontalSpan = 2;
			vformData.heightHint = 150;
			vform.setLayoutData(vformData);
			BaseComposite columnComposite = new BaseComposite(column,vform);
			
			Composite composite = toolkit.createComposite(vform);
			composite.setLayout(new GridLayout(1, true));
			composite.setLayoutData(new GridData(GridData.FILL_BOTH));
			composite.setBackground(bgcolor);
			
			Group group1 = new Group(composite, SWT.NULL);
			group1.setText("栏位配置");
			group1.setLayout(new GridLayout(2, false));
			group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			
			Label label2= toolkit.createLabel(group1, "CID:",SWT.RIGHT); 
			label2.setBackground(bgcolor);
			Text cid = new Text(group1, SWT.BORDER);
			cid.setLayoutData(new GridData(300, 15));
			columnComposite.getDataBind().bind("cid",cid);
			
			Label label3=  toolkit.createLabel(group1, "标题:",SWT.RIGHT); 
			label3.setBackground(bgcolor);
			Text caption = new Text(group1, SWT.BORDER);
			caption.setLayoutData(new GridData(300, 15));
			columnComposite.getDataBind().bind("caption",caption);
			
			Label label4= toolkit.createLabel(group1, "宽度:",SWT.RIGHT); 
			label4.setBackground(bgcolor);
			Text width = new Text(group1, SWT.BORDER);
			width.setLayoutData(new GridData(300, 15));
			columnComposite.getDataBind().bind("width",width);
			
			Label label5 = toolkit.createLabel(group1, "编辑:",SWT.RIGHT);
			label5.setBackground(bgcolor);
			Combo combo1 = new Combo(group1, SWT.READ_ONLY);
			combo1.setItems(new String[]{"--","静态","动态"});
			combo1.select(0);
			combo1.setLayoutData(new GridData(100, 24));
			combo1.setData("0","");
			combo1.setData("1","static");
			combo1.setData("2","dynamic");
			columnComposite.getDataBind().bind("edit",combo1);
			
			//控件 
			BaseDataEntity data = new BaseDataEntity();
			Composite ctrCmp = toolkit.createComposite(composite);
			ctrCmp.setBackground(bgcolor);
			StackLayout stackLayout = new StackLayout();
			ctrCmp.setLayout(stackLayout);
			GridData ctrCmpData = new GridData(GridData.FILL_HORIZONTAL);
//			ctrCmpData.horizontalSpan=2;
			ctrCmp.setLayoutData(ctrCmpData);
			
			//取出静态控件
			BaseComposite staticBaseComposite = getStaticCtrComposite(ctrCmp,columnComposite);
			
			//取出动态控件
			BaseComposite dynamicBaseComposite = getDynamicCtrComposite(ctrCmp,columnComposite);
			
			data.spov("columnComposite", columnComposite);
			data.spov("ctrCmp", ctrCmp);
			data.spov("staticBaseComposite", staticBaseComposite);
			data.spov("dynamicBaseComposite", dynamicBaseComposite);
			
			combo1.addModifyListener(new PModifyListener(data){
				@Override
				public void modifyText(ModifyEvent event) {
					Combo combo1 						= (Combo)event.getSource();
					String value 						= (String)combo1.getData(StringHelper.getValue(combo1.getSelectionIndex()));
					Composite ctrCmp 					= (Composite)this.gpov("ctrCmp");
					StackLayout stackLayout 			= (StackLayout)ctrCmp.getLayout();
					BaseComposite columnComposite 		= (BaseComposite)this.gpov("columnComposite");
					BaseComposite staticBaseComposite 	= (BaseComposite)this.gpov("staticBaseComposite");
					BaseComposite dynamicBaseComposite 	= (BaseComposite)this.gpov("dynamicBaseComposite");
					
					if(StringHelper.IsNullOrEmpty(value)){
						stackLayout.topControl = null;
						BaseDataBind editorBind = (BaseDataBind)columnComposite.getDataBind().getCtr("editor");
						if(editorBind != null){
							editorBind.reset();
						}
						columnComposite.getDataBind().unbind("editor");
					}else{
						if("static".equals(value)){
							stackLayout.topControl = staticBaseComposite.getComposite();
						}else if("dynamic".equals(value)){
							stackLayout.topControl = dynamicBaseComposite.getComposite();
						}else {
							stackLayout.topControl = null;
						}
						
						BaseDataBind editorBind = (BaseDataBind)columnComposite.getDataBind().getCtr("editor");
						if(editorBind != null){
							editorBind.reset();
						}else{
							XMLNode columnNode = columnComposite.getNode();
							XMLNode editorNode = columnNode.GetChildNodeByNodeName("editor");
							if(editorNode != null){
								if("static".equals(value)){
									staticBaseComposite.getDataBind().setRecord(editorNode.getBaseDataEntity(true, false));
								}else if("dynamic".equals(value)){
									dynamicBaseComposite.getDataBind().setRecord(editorNode.getBaseDataEntity(true, false));
								}
							}
						}
					}
					ctrCmp.layout();
				}
			});
			
			TableColumnActionGroup actionGroup1 = new TableColumnActionGroup(columnComposite,detailPage.getFileHandle());
			actionGroup1.fillContextMenu(new MenuManager());
			ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
			toolbar1.setBackground(detailPage.getDetailSection().getDisplay()
					.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
			ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
			actionGroup1.fillActionToolbars(toolbarManager);
			
			vform.setContent(composite);
			vform.setTopLeft(toolbar1);
			
			this.repeatCp.put(id, columnComposite);
			return columnComposite;
		}
	}
	
	/**
	 * @param parent
	 * @param columnComposite
	 * @return
	 * 静态控件配置
	 */
	public BaseComposite getStaticCtrComposite(Composite parent,BaseComposite columnComposite){
		Color bgcolor = detailPage.getDetailSection().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		FormToolkit toolkit = detailPage.getMform().getToolkit();
		Group group= new Group(parent, SWT.NULL);
		group.setText("静态控件配置");
		group.setLayout(new GridLayout(2, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		BaseComposite staticBaseComposite = new BaseComposite(null, group);
		
		Label label0 = toolkit.createLabel(group, "控件类型:",SWT.RIGHT);
		label0.setBackground(bgcolor);
		Combo combo1 = new Combo(group, SWT.READ_ONLY);
		combo1.setItems(new String[]{"--","文本(Text)","下拉框(Select)","单选框(Radio)","复选框(Checkbox)"});
		combo1.select(0);
		combo1.setLayoutData(new GridData(100, 24));
		combo1.setData("0","");
		combo1.setData("1","text");
		combo1.setData("2","select");
		combo1.setData("3","radio");
		combo1.setData("4","checkbox");
		staticBaseComposite.getDataBind().bind("editType",combo1);
		
		Composite ctrCmp = toolkit.createComposite(group);
		ctrCmp.setBackground(bgcolor);
		StackLayout stackLayout = new StackLayout();
		ctrCmp.setLayout(stackLayout);
		GridData ctrCmpData = new GridData(GridData.FILL_HORIZONTAL);
		ctrCmpData.horizontalSpan=2;
		ctrCmp.setLayoutData(ctrCmpData);
		
		BaseDataEntity data = new BaseDataEntity();
		data.spov("ctrCmp", ctrCmp);
		data.spov("columnComposite", columnComposite);
		data.spov("group", group);
		data.spov("staticBaseComposite", staticBaseComposite);
		
		combo1.addModifyListener(new PModifyListener(data){
			@Override
			public void modifyText(ModifyEvent event) {
				Combo combo1 = (Combo)event.getSource();
				String type = (String)combo1.getData(StringHelper.getValue(combo1.getSelectionIndex()));
				BaseComposite columnComposite = (BaseComposite)this.gpov("columnComposite");
				Composite ctrCmp = (Composite)this.gpov("ctrCmp");
				Group group = (Group)this.gpov("group");
				BaseComposite staticBaseComposite = (BaseComposite)this.gpov("staticBaseComposite");
				
				StackLayout stackLayout = (StackLayout)ctrCmp.getLayout();
				if(StringHelper.IsNullOrEmpty(type)){
					stackLayout.topControl = null;
					staticBaseComposite.getDataBind().reset();
					columnComposite.getDataBind().unbind("editor");
				}else{
					Composite ctr = getCtrConfig(ctrCmp, type,staticBaseComposite,columnComposite.getNode()).getComposite();
					staticBaseComposite.getDataBind().bind("editType",combo1);
					
					if(staticBaseComposite.getNode() == null){
						staticBaseComposite.setNode(columnComposite.getNode().GetChildNodeByNodeName("editor"));
					}
					
//					//给编辑控件设置值
//					XMLNode editorNode = staticBaseComposite.getNode();
//					if(editorNode !=null){
//						BaseDataEntity record = editorNode.getBaseDataEntity(true, false);
//						record.spov("editType", combo1.getData(String.valueOf(combo1.getSelectionIndex())));
//						record.spsv("width", "100px");
//						//staticBaseComposite.getDataBind().setRecord(record);
//					}
					
					columnComposite.getDataBind().bind("editor", staticBaseComposite.getDataBind());
					stackLayout.topControl = ctr;
				}
				ctrCmp.layout();
				ViewForm vform = (ViewForm)columnComposite.getComposite();
				((Composite)vform.getContent()).layout();
			}
		});
		return staticBaseComposite;
	}
	
	/**
	 * @param parent
	 * @param type
	 * @return
	 * 要把控件的类型,返回控件的配置面板
	 */
	public BaseComposite getCtrConfig(Composite parent,String type,BaseComposite staticBaseComposite,XMLNode columnNode){
		if(StringHelper.IsNullOrEmpty(type))return null;
		String key = "ctr_"+type+columnNode.getID();
		if(this.ctrMap == null)this.ctrMap = new HashMap<String, BaseComposite>();
		
		BaseComposite crtBaseComposite = null;
		if(this.ctrMap.containsKey(key)){
			crtBaseComposite = this.ctrMap.get(key);
		}else{
			Color bgcolor = detailPage.getDetailSection().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
			FormToolkit toolkit = detailPage.getMform().getToolkit();
			Composite composite = toolkit.createComposite(parent);
			composite.setLayout(new GridLayout(2, false));
			composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			composite.setBackground(bgcolor);
			crtBaseComposite = new BaseComposite(columnNode, composite);
			String nodeName = "input";
			if("select".equalsIgnoreCase(type)){
				nodeName = "select";
			}
			
			BaseDataBind dataBind = crtBaseComposite.getDataBind(); 
			Label label1=  toolkit.createLabel(composite, "ID:",SWT.RIGHT); 
			label1.setBackground(bgcolor);
			Text id = new Text(composite, SWT.BORDER);
			id.setLayoutData(new GridData(200, 15));
			dataBind.bind("id", id);	
			
			Label label2=  toolkit.createLabel(composite, "名称:",SWT.RIGHT); 
			label2.setBackground(bgcolor);
			Text name = new Text(composite, SWT.BORDER);
			name.setLayoutData(new GridData(200, 15));
			dataBind.bind("name", name);
			
			Label valueLabel=  toolkit.createLabel(composite, "值:",SWT.RIGHT); 
			valueLabel.setBackground(bgcolor);
			Text value = new Text(composite, SWT.BORDER);
			
			value.setLayoutData(new GridData(200, 15));
			dataBind.bind("value", value);
			
			if("text".equalsIgnoreCase(type)){
				value.setText("ret.grsv(\""+columnNode.getID()+"\")");
			}
			
			//radio ,checkbox 不需要宽度
			if(!("radio".equalsIgnoreCase(type) || "checkbox".equalsIgnoreCase(type))){
				Label label3=  toolkit.createLabel(composite, "宽度:",SWT.RIGHT);
				label3.setBackground(bgcolor);
				Text width = toolkit.createText(composite, "100px",  SWT.BORDER);
				width.setLayoutData(new GridData(200, 15));
				dataBind.bind("width", width);
			}else{
				dataBind.unbind("width");
			}
			
			if("checkbox".equalsIgnoreCase(type)){
				Label label = toolkit.createLabel(composite, "带隐藏框\r\n(WithHidden):",SWT.LEFT);
				label.setBackground(bgcolor);
				
				ListGroup withHidden = new ListGroup(composite, "singlecheck");
				withHidden.setLayout(new GridLayout(2, false));
				withHidden.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
				withHidden.setData("xtype", "check");
				Button btn1 = withHidden.getNewButton();
				btn1.setData(true);
				dataBind.bind("withHidden", withHidden);
			}else{
				dataBind.unbind("withHidden");
			}
			
			if("select".equalsIgnoreCase(type)
					||"radio".equalsIgnoreCase(type)
					||"checkbox".equalsIgnoreCase(type)){
				Label label5=  toolkit.createLabel(composite, "代码表(CodeList):",SWT.RIGHT);
				label5.setBackground(bgcolor);
				Text codeList = toolkit.createText(composite, "",  SWT.BORDER);//|SWT.H_SCROLL
				codeList.setLayoutData(new GridData(400, 15));
				dataBind.bind("codeList", codeList);
				
				Label label6 = toolkit.createLabel(composite, "DataSource:",SWT.RIGHT);
				label6.setBackground(bgcolor);
				Combo combo1 = new Combo(composite, SWT.READ_ONLY);
				String[] datasources = QMDao.getPanAllDataSource(true);
				combo1.setItems(datasources);
				combo1.select(0);
				combo1.setLayoutData(new GridData(100, 24));
				combo1.setData("0","");
				for (int i = 0; i < datasources.length; i++) {
					combo1.setData(String.valueOf(i+1), datasources[i]);
				}
				dataBind.bind("dataSource", combo1);
				
				Label label7=  toolkit.createLabel(composite, "SQL:",SWT.RIGHT);
				label7.setBackground(bgcolor);
				Text sql = toolkit.createText(composite, "",  SWT.BORDER);
				sql.setLayoutData(new GridData(400, 45));
				dataBind.bind("sql", sql);
			}else{
				dataBind.unbind("dataSource");
				dataBind.unbind("codeList");
				dataBind.unbind("sql");
			}
			
			this.ctrMap.put(key, crtBaseComposite);
		}
		crtBaseComposite.getDataBind().cloneTo(staticBaseComposite.getDataBind(), true);
		return crtBaseComposite;
	}
	
	/**
	 * @param parent
	 * @param columnComposite
	 * @return
	 * 动态控件配置
	 */
	public BaseComposite getDynamicCtrComposite(Composite parent,BaseComposite columnComposite){
		Group group= new Group(parent, SWT.NULL);
		group.setText("动态控件配置");
		group.setLayout(new GridLayout(2, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		BaseComposite dynamicBaseComposite = new BaseComposite(null, group);
		return dynamicBaseComposite;
	}
	
	/**
	 * @param nodeName
	 * @return
	 * 页面节点面板
	 */
	public BaseComposite getPageComposite(XMLNode pageNode){
		FormToolkit toolkit = detailPage.getMform().getToolkit();
		ViewForm vform = new ViewForm((Composite)detailPage.getDetailSection().getClient(), SWT.NONE);
		vform.setLayout(new GridLayout(1, true));
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vform.setLayoutData(vformData);
		BaseComposite pageComposite = new BaseComposite(pageNode,vform);
		
		Composite composite = toolkit.createComposite(vform);
		composite.setLayout(new GridLayout(1, true));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		composite.setBackground(detailPage.getDetailSection().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		
		
		Group group1 = new Group(composite, SWT.NULL);
		group1.setText("页面属性");
		group1.setToolTipText("页面属性");
		group1.setLayout(new GridLayout(2, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label label2= new Label(group1, SWT.RIGHT);
		label2.setText("页面名称:");
		Text pageName = new Text(group1, SWT.BORDER);
//		pageName.addFocusListener(PFocusListener.getPFocusListener(detailPage));
//		pageName.addModifyListener(PModifyListener.getPModifyListener(detailPage));
		pageName.setLayoutData(new GridData(300, 15));
		pageComposite.getDataBind().bind("pageName",pageName);
		
		Label label3= new Label(group1, SWT.RIGHT);
		label3.setText("页面标题:");
		Text pageTitle = new Text(group1, SWT.BORDER);
//		pageTitle.addModifyListener(PModifyListener.getPModifyListener(detailPage));
//		pageTitle.addFocusListener(PFocusListener.getPFocusListener(detailPage));
		pageTitle.setLayoutData(new GridData(300, 15));
		pageComposite.getDataBind().bind("pageTitle",pageTitle);
		
		PageActionGroup actionGroup1 = new PageActionGroup(pageComposite,detailPage.getFileHandle());
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		toolbar1.setBackground(detailPage.getDetailSection().getDisplay()
				.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager);
		
		vform.setContent(composite);
		vform.setTopLeft(toolbar1);
		return pageComposite;
	}
	
	
	/**
	 * @param table
	 * @return
	 * 表格节点面板
	 */
	public BaseComposite getTableComposite(XMLNode table){
		String id = table.getID();
		if(StringHelper.IsNullOrEmpty(id))
			id = table.gpsv("id");
		if(repeatCp.containsKey(id)){
			BaseComposite gridComposite = this.repeatCp.get(id);
			XMLNode node = 	detailPage.getCurNode();
			PTableViewer tv2 = (PTableViewer)gridComposite.getDataInstance().gpov("tv2");//已选栏位表格查看器
			tv2.setInput(node.getChildBaseDataEntities());
			return gridComposite;
		}else{
			TabFolder tabFolder = new TabFolder((Composite)detailPage.getDetailSection().getClient(), SWT.NONE);
			BaseComposite gridComposite = new BaseComposite(table,tabFolder);
			
			TabItem tabItme1 = new TabItem(tabFolder, SWT.NONE);
			tabItme1.setText("表格界面");
			ViewForm vform1 = this.getTableUIViewForm(gridComposite); 
			tabItme1.setControl(vform1);
			
			TabItem tabItme2 = new TabItem(tabFolder, SWT.NONE);
			tabItme2.setText("代码辅助");
			
	        this.repeatCp.put(id, gridComposite);
			return gridComposite;
		}
	}
	
	public ViewForm getTableUIViewForm(BaseComposite gridComposite){
		FormToolkit toolkit = detailPage.getMform().getToolkit();
		TabFolder tabFolder = (TabFolder)gridComposite.getComposite();
		ViewForm vform = new ViewForm(tabFolder, SWT.NONE);
		vform.setLayout(new GridLayout(1, true));
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vform.setLayoutData(vformData);
		
		Composite composite = toolkit.createComposite(vform);
		composite.setLayout(new GridLayout(1, true));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		composite.setBackground(detailPage.getDetailSection().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		
		Section st1 = toolkit.createSection(composite, Section.TWISTIE|Section.TITLE_BAR);
        st1.setText("表格配置");
        st1.setLayout(new GridLayout(1,false));
        st1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        //创建一个面板 作为内容折叠区域放置的控件
        Composite cs = toolkit.createComposite(st1);
        cs.setLayout( new GridLayout(1,false));
        cs.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        st1.setClient(cs);
        
        createDateGridPropertyComposite(gridComposite, st1);
        
		// 为折叠面板添加展开 折叠的监听器
		st1.addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent e) {
				// 根据部件的新尺寸重新定位和更新滚动条
				detailPage.getMform().reflow(true);
			}
		});
		st1.setExpanded(true);
		
		
		Section st2 = toolkit.createSection(composite, Section.TWISTIE|Section.TITLE_BAR);
		st2.setText("表格栏位信息");
		st2.setLayout(new GridLayout(1,false));
		st2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		  //创建一个面板 作为内容折叠区域放置的控件
        Composite cs2 = toolkit.createComposite(st2);
        cs2.setLayout(new GridLayout(1,false));
        cs2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        st2.setClient(cs2);
        //创建数据源区域的面板
        createDataSourceComposite(gridComposite, st2);
		 //创建表格信息面板
        createDateGridComposite(gridComposite, st2);
        
        //为折叠面板添加展开 折叠的监听器
		st2.addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent e) {
				// 根据部件的新尺寸重新定位和更新滚动条
				detailPage.getMform().reflow(true);
			}
		});
		st2.setExpanded(true);
		
		TableActionGroup actionGroup1 = new TableActionGroup(gridComposite,detailPage.getFileHandle());
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		toolbar1.setBackground(detailPage.getDetailSection().getDisplay()
				.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager);
		
		vform.setContent(composite);
		vform.setTopLeft(toolbar1);
		return vform;
	}
	
	
	/**
	 * @param gridComposite
	 * @param st2
	 * 创建表格属性面板
	 */
	public void createDateGridPropertyComposite(BaseComposite gridComposite,Section st1){
		Color bgcolor = detailPage.getDetailSection().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		FormToolkit toolkit = detailPage.getMform().getToolkit();
		
		Composite parent = toolkit.createComposite((Composite)st1.getClient());
		parent.setLayout( new GridLayout(4,false));
		parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		parent.setBackground(bgcolor);
		
		Label lb0 = toolkit.createLabel(parent, "表格ID:",SWT.RIGHT);
		lb0.setBackground(bgcolor);
		Text text0 = toolkit.createText(parent, "", SWT.BORDER);
		text0.setLayoutData(new GridData(100, 15));
		text0.setEditable(false);
		text0.setEnabled(false);
		gridComposite.getDataBind().bind("id", text0);
		
		Label lb01 = toolkit.createLabel(parent, "表格名称:",SWT.RIGHT);
		lb01.setBackground(bgcolor);
		Text labelText = toolkit.createText(parent, "", SWT.BORDER);
		labelText.setLayoutData(new GridData(100, 15));
		gridComposite.getDataBind().bind("labelText", labelText);
		
		
		Label lb1 = toolkit.createLabel(parent, "数据源:",SWT.RIGHT);
		lb1.setBackground(bgcolor);
		Composite cps1 =toolkit.createComposite(parent);
		cps1.setBackground(bgcolor);
		cps1.setLayout(new GridLayout(6, false));
		GridData cps1data = new GridData(GridData.FILL_HORIZONTAL);
		cps1data.horizontalSpan=3;
		cps1.setLayoutData(cps1data);
		
		Text dataSet = toolkit.createText(cps1, "", SWT.BORDER);
		gridComposite.getDataBind().bind("dataSet", dataSet);
		dataSet.setLayoutData(new GridData(100, 15));
		gridComposite.getDataBind().bind("dataSet", dataSet);
		Button defaultDataSet = toolkit.createButton(cps1, "默认数据源", SWT.CHECK);
		defaultDataSet.setBackground(bgcolor);
		defaultDataSet.setData(true);
		defaultDataSet.setData("dataSet", dataSet);
		
		defaultDataSet.addSelectionListener(new PSelectionListener(null){
			@Override
			public void widgetSelected(SelectionEvent event) {
				Button btn1 = (Button)event.getSource();
				Text dataSet = (Text)btn1.getData("dataSet");
				if(btn1.getSelection()){
					dataSet.setText("default");
					dataSet.setEditable(false);
					dataSet.setEnabled(false);
				}else{
					dataSet.setText("");
					dataSet.setEditable(true);
					dataSet.setEnabled(true);
				}
			}
		});
		gridComposite.getDataBind().bind("defaultDataSet", defaultDataSet);
		
		Button loopFisrt = toolkit.createButton(cps1, "空值循环一次", SWT.CHECK);
		loopFisrt.setBackground(bgcolor);
		loopFisrt.setData(true);
		gridComposite.getDataBind().bind("loopFisrt", loopFisrt);
		
		Button view = toolkit.createButton(cps1, "锁表头", SWT.CHECK);
		view.setBackground(bgcolor);
		view.setData("fixedRow");
		gridComposite.getDataBind().bind("view", view);
		
		Label lb3 = toolkit.createLabel(parent, "工具栏:",SWT.RIGHT);
		lb3.setBackground(bgcolor);
		Button toolbar = toolkit.createButton(parent, "显示默认工具栏", SWT.CHECK);
		toolbar.setBackground(bgcolor);
		toolbar.setData(true);
		gridComposite.getDataBind().bind("toolbar", toolbar);
		
		Group group1 = new Group(parent, SWT.NULL);
		group1.setText("表格工具栏配置");
		group1.setLayout(new GridLayout(2, false));
		GridData group1data = new GridData(GridData.FILL_HORIZONTAL);
		group1data.horizontalSpan = 4;
		group1.setLayoutData(group1data);
	}

	/**
	 * @param gridComposite
	 * @param cs
	 * 创建数据源区域的面板
	 */
	public void createDataSourceComposite(BaseComposite gridComposite,Section st2){
		FormToolkit toolkit = detailPage.getMform().getToolkit();
		ExpandableComposite ec = toolkit.createExpandableComposite((Composite)st2.getClient(), ExpandableComposite.TREE_NODE|ExpandableComposite.NO_TITLE_FOCUS_BOX);
		ec.setData("st2",st2);
		ec.setText("根据SQL带出表格栏位");	
		ec.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		ec.addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent e) {
				// 根据部件的新尺寸重新定位和更新滚动条
				Composite ec = (ExpandableComposite)e.getSource();
				ec.layout();
				Section st2 = (Section)ec.getData("st2");
				st2.setExpanded(true);//面板展开会自动 刷新form的布局.
//				detailPage.getMform().reflow(false);
			}
		});
		
//		Group group1 = new Group(ec, SWT.NULL);
		Composite group1 = toolkit.createComposite(ec,SWT.NONE);
//		group1.setText("数据源");
		group1.setLayout(new GridLayout(3, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		ec.setClient(group1);

		Label labe11 = new Label(group1, SWT.RIGHT);
		labe11.setText("DataSource:");
		Combo combo1 = new Combo(group1, SWT.READ_ONLY);
		String[] datasources = QMDao.getPanAllDataSource(false);
		combo1.setItems(datasources);

		combo1.select(1);
		combo1.setLayoutData(new GridData(100, 24));
		for (int i = 0; i < datasources.length; i++) {
			combo1.setData(String.valueOf(i), datasources[i]);
		}
		gridComposite.getDataBind().bind("dataSource", combo1);

		Button button1 = new Button(group1, SWT.NONE);
		button1.setText("语句分析");

		Label labe12 = new Label(group1, SWT.RIGHT);
		labe12.setText("SQl:");
		Text text2 = new Text(group1, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		GridData text2GridData = new GridData(GridData.FILL_HORIZONTAL);
		text2GridData.horizontalSpan = 2;
		text2GridData.heightHint = 50;
		text2.setLayoutData(text2GridData);
		gridComposite.getDataBind().bind("sql", text2);

		Label labe13 = new Label(group1, SWT.RIGHT);
		labe13.setText("可选栏位:");

		ViewForm vform = new ViewForm(group1, SWT.NONE);
		// vform.setLayout(new FillLayout());
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vform.setLayoutData(vformData);
		
		
		PTableViewer tv1 = createTableViewer1(vform);
		CheckboxTableViewer ctv1 = new CheckboxTableViewer(tv1.getTable());
		//存放查看器
		BaseDataEntity data1 = gridComposite.getDataInstance();
		data1.spov("tv1", tv1);
		data1.spov("ctv1", ctv1);

		tv1.setContentProvider(new PllTableContentProvider()); // 内容器
		tv1.setLabelProvider(new PllTableLabelProvider(tv1));// 标签器
		// candidateTable = newCandidateTable(group1);

		SeachActionGroup1 actionGroup1 = new SeachActionGroup1(gridComposite,detailPage.getFileHandle());
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		toolbar1.setBackground(detailPage.getDetailSection().getDisplay()
				.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager);

		vform.setContent(tv1.getControl());
		vform.setTopLeft(toolbar1);
		
		BaseDataEntity data2 = new BaseDataEntity();
		data2.spov("tv", tv1);
		data2.spov("bind", gridComposite.getDataBind());

		button1.addMouseListener(new PMouseListener(data2) {
			public void mouseUp(MouseEvent paramMouseEvent) {
				QMDao qmDao = QMDao.getInstance();
				BaseDataBind bind = (BaseDataBind) this.gpov("bind");
				PTableViewer tv1 = (PTableViewer) this.gpov("tv");
				tv1.setInput(qmDao.getColumnsInfo(bind.getValue("dataSource"),
						bind.getValue("sql")));
				// tv1.getPTable().ReLoadRecord(qmDao.getColumnsInfo(bind.getValue("dataSource"),bind.getValue("sql")));
			}
		});
	}
	
	/**
	 * @param group1
	 * @return
	 * 可选栏位的表格查看器
	 */
	public PTableViewer createTableViewer1(ViewForm vform){
		PTableViewer tv1 = new PTableViewer(new PTable(vform, SWT.CHECK | SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION));
		PTable table = tv1.getPTable();
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
//		tableGridData.horizontalSpan=2;
//		tableGridData.heightHint = 100;
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true); // 表头显示
		table.setLinesVisible(true); // 表格线显示
		
		TableLayout tLayout = new TableLayout();
		table.setLayout(tLayout);
		
		tLayout.addColumnData(new ColumnWeightData(20));
		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("cid");
		
		tLayout.addColumnData(new ColumnWeightData(30));
		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		
		table.reloadColumns();
		return tv1;
	}
	
	/**
	 * @param gridComposite
	 * @param cs
	 * 创建表格信息面板
	 */
	public void createDateGridComposite(BaseComposite gridComposite,Section st2){
		Group group1 = new Group((Composite)st2.getClient(), SWT.NULL);
		group1.setText("已选栏位");
		group1.setLayout(new GridLayout(2, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		ViewForm vform = new ViewForm(group1, SWT.NONE);
		// vform.setLayout(new FillLayout());
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vform.setLayoutData(vformData);

		PTableViewer tv2 = createTableViewer2(vform);
		CheckboxTableViewer ctv2 = new CheckboxTableViewer(tv2.getTable());
		//存放查看器
		BaseDataEntity data1 = gridComposite.getDataInstance();
		data1.spov("tv2", tv2);
		data1.spov("ctv2", ctv2);
		
		tv2.setContentProvider(new PllTableContentProvider()); // 内容器
		tv2.setLabelProvider(new PllTableLabelProvider(tv2));// 标签器
		
		XMLNode node = 	detailPage.getCurNode();
		tv2.setInput(node.getChildBaseDataEntities());
		
		SelectedColumnsActionGroup actionGroup1 = new SelectedColumnsActionGroup(gridComposite,detailPage.getFileHandle());
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		toolbar1.setBackground(detailPage.getDetailSection().getDisplay()
				.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager);
		
		vform.setContent(tv2.getControl());
		vform.setTopLeft(toolbar1);
	}
	
	/**
	 * @param group1
	 * @return
	 *已选栏位的表格查看器
	 */
	public PTableViewer createTableViewer2(ViewForm vform){
		PTableViewer tv2 = new PTableViewer(new PTable(vform, SWT.CHECK | SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION));
		PTable table = tv2.getPTable();
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true); // 表头显示
		table.setLinesVisible(true); // 表格线显示
		
		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("cid");
		col1.setName("cid");
		col1.setWidth(80);
		
		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		col2.setWidth(120);
		table.reloadColumns();
		
		PTableColumn col3 = new PTableColumn(table, SWT.NONE);
		col3.setText("宽度");
		col3.setName("width");
		col3.setWidth(60);
		table.reloadColumns();
		
		tv2.setColumnProperties(new String[]{"cid","caption","width"});
		CellEditor[] editors = new CellEditor[3];
		editors[0] = new TextCellEditor(table,SWT.NONE);;
		editors[1] = new TextCellEditor(table,SWT.NONE);
		editors[2] = new TextCellEditor(table,SWT.NONE);
		tv2.setCellEditors(editors);
		List list = new ArrayList();
		list.add("cid");
		list.add("caption");
		list.add("width");
		tv2.setCellModifier(new SelectColumnsTableCellModifier(tv2,list));
		return tv2;
	}
	
	
	
}
